<?php
/**
 * はてなウェブサービス用PHPクラス
 *
 * For details and licence see:
 * http://code.google.com/p/php-hatena-api/
 */

if(!class_exists('cHatenaApi')){
    class cHatenaApi{
        protected $_sUsername;
        protected $_sPassword;
        var $oHttpRequest;
        var $sRootAtomEndPoint = "http://b.hatena.ne.jp";

        //コンストラクタを定義
        public function __construct($sUsername,$sPassword){
            $this->cHatenaApi($sUsername,$sPassword);
        }
        public function cHatenaApi($sUsername,$sPassword){
            if(!class_exists('HTTP_Request')) die("PEAR HTTP_Request required to use cHatenaApi.");

            $this->setAccount($sUsername,$sPassword);
            $this->_setRequestHeader();

        }

        public function setAccount($sUsername,$sPassword){
            $this->_sUsername  = $sUsername;
            $this->_sPassword = $sPassword;
        }
        
        public function setMethod($mMethod){
            switch(TRUE){
                case $mMethod == HTTP_REQUEST_METHOD_POST:
                case strtolower($mMethod) == 'post':
                    $this->oHttpRequest->setMethod(HTTP_REQUEST_METHOD_POST);
                    break;
                case $mMethod == HTTP_REQUEST_METHOD_GET:
                case strtolower($mMethod) == 'get':
                    $this->oHttpRequest->setMethod(HTTP_REQUEST_METHOD_GET);
                    break;
                default:
                    
            }
        }

        private function _setRequestHeader(){
            $this->oHttpRequest = new HTTP_Request(); //TODO:すでにインスタンスが作られている場合の処理が足りない
            $this->oHttpRequest->addHeader('Accept', 'application/x.atom+xml, application/xml, text/xml, */*');
            $this->oHttpRequest->addHeader('Authorization', 'WSSE profile="UsernameToken"');
            $this->oHttpRequest->addHeader('X-WSSE', $this->_getWSSE() );
            $this->oHttpRequest->addHeader('Content-Type', 'application/x.atom+xml');
        }

        private function _getWSSE() {
            $sUsername = $this->_sUsername;
            $sPassword = $this->_sPassword;

            $sTimeCreated    = date('Y-m-d\TH:i:s\Z'); //ISO-8601 format
            $sNonce          = pack('H*', sha1(md5(time()))); //Security Token per each HTTP request
            $sPasswordDigest = base64_encode(pack('H*', sha1($sNonce . $sTimeCreated . $sPassword)));
            $sNonce_base64   = base64_encode($sNonce);

            return "UsernameToken Username=\"{$sUsername}\", PasswordDigest=\"{$sPasswordDigest}\", Nonce=\"{$sNonce_base64}\", Created=\"{$sTimeCreated}\"";
        }

        /**
         * =================================
         *  はてなブックマーク用メッソッド
         * =================================
         */
        public function getBookmark(){
            $this->setMethod('GET');
            $sEndPoint_FeedURI = '/atom/feed';
            $this->oHttpRequest->setURL($this->sRootAtomEndPoint . $sEndPoint_FeedURI);
            if( $this->oHttpRequest->sendRequest() ){
                $oResponse = simplexml_load_string( $this->oHttpRequest->_response->_body );
                foreach($oResponse->entry as $oItem){
                    $aTemp = (array) $oItem;
                    $aSubject = (array) $oItem->children( "http://purl.org/dc/elements/1.1/" );
                    $aTemp += $aSubject;
                    $aItems[] = $aTemp;
                }
                return $aItems;
            }
            else{
                return FALSE;
            }
        }

    }/* /cHatenaApi */
}
